package beans.cadastro.operacional;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import javax.faces.component.UICommand;
import javax.persistence.EntityExistsException;
import javax.persistence.Query;
import javax.persistence.EntityManager;
import javax.persistence.Persistence;
import javax.persistence.PersistenceContext;
import pojo.cadastro.operacional.Marca;

public class MarcaBean {

    @PersistenceContext
    private EntityManager mgr;

    // pojo
    private long id;
    private String descricao;

    // Campos de Pesquisa
    private String descricaoPesq;

    // listas
    private List<Marca> listaMarcas;

    // botoes de comandos
    private UICommand btnAlterar;
    private UICommand btnExcluir;

    public MarcaBean() {
    }

    public String pesquisar() {
        carregar();
        return "pesquisa";
    }

    public String retornar() {
        return "pesquisa";
    }

    public String carregar() {

        listaMarcas = Collections.EMPTY_LIST;
        String sql = "SELECT m FROM Marca m WHERE m.id IS NOT NULL ";
        if (!this.descricaoPesq.equals(""))
            sql += "AND m.descricao LIKE :descricaoPesq ";
        sql += "ORDER BY m.descricao";

        mgr = Persistence.createEntityManagerFactory("lojaPU").createEntityManager();
        Query q1 = mgr.createQuery(sql);

        if (!this.descricaoPesq.equals(""))
            q1.setParameter("descricaoPesq", "%"+this.descricaoPesq+"%");

        listaMarcas = q1.getResultList();
        mgr.close();

        return null;
    }

    public String salvar() {
        Marca m = new Marca();

        if (id != 0) {
            m.setId(id);
        }

        m.setDescricao(this.descricao);
        m.setDataCadastro(new Date(System.currentTimeMillis()));

        mgr = Persistence.createEntityManagerFactory("lojaPU").createEntityManager();
        mgr.getTransaction().begin();
        try {
            if (m.getId() == 0) {
                mgr.persist(m);
            } else {
                mgr.merge(m);
            }
            mgr.getTransaction().commit();
        } catch (EntityExistsException e) {
            mgr.getTransaction().rollback();
        } finally {
            mgr.close();
        }

        listaMarcas = Collections.EMPTY_LIST;

        return "pesquisa";
    }

    public String incluir() {
        id = 0;
        descricao = "";

        return "cadastro";
    }

    public String alterar() {
        mgr = Persistence.createEntityManagerFactory("lojaPU").createEntityManager();
        Marca m = mgr.find(Marca.class, Long.parseLong(btnAlterar.getValue().toString()));

        id = m.getId();
        descricao = m.getDescricao();
        
        return "cadastro";
    }

    public String excluir() {
        mgr = Persistence.createEntityManagerFactory("lojaPU").createEntityManager();
        Marca m = mgr.find(Marca.class, Long.parseLong(btnExcluir.getValue().toString()));

        mgr.getTransaction().begin();
        try {
            mgr.remove(m);
            mgr.getTransaction().commit();
        } catch (EntityExistsException e) {
            mgr.getTransaction().rollback();
        } finally {
            mgr.close();
        }
        carregar();

        return null;
    }

    // Getters and Setters
    
    public UICommand getBtnAlterar() {
        return btnAlterar;
    }

    public void setBtnAlterar(UICommand btnAlterar) {
        this.btnAlterar = btnAlterar;
    }

    public UICommand getBtnExcluir() {
        return btnExcluir;
    }

    public void setBtnExcluir(UICommand btnExcluir) {
        this.btnExcluir = btnExcluir;
    }

    public String getDescricao() {
        return descricao;
    }

    public void setDescricao(String descricao) {
        this.descricao = descricao;
    }

    public String getDescricaoPesq() {
        return descricaoPesq;
    }

    public void setDescricaoPesq(String descricaoPesq) {
        this.descricaoPesq = descricaoPesq;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public List<Marca> getListaMarcas() {
        return listaMarcas;
    }

    public void setListaMarcas(List<Marca> listaMarcas) {
        this.listaMarcas = listaMarcas;
    }

}